// Copyright (C) 2011 by Christopher Diggins
// Usage is permitted under the terms of the MIT License 1.0. 	

/** \file 	
	Entry point of CVM implementation.
*/

#ifdef _DEBUG
#define TESTS 1
#endif

#include "cvm.h"

#include <stdio.h>

#include <string.h>

#ifdef TESTS
#include "tests.inl"
#endif

void load_file(const char* path, Array* a) 
{
	FILE* f = fopen(path, "r");	
	byte b = fgetc(f);
	for (; !feof(f); b = fgetc(f)) 
		array_push(a, &b, sizeof(b));
	fclose(f);
}

void load_and_run(VM* vm, char* filename)
{
	Array a;
	array_init(&a, sizeof(byte));
	vm_init(vm);	
	load_file(filename, &a);
	// TODO:
	// deserialize_vm(vm, obj_begin(a));
	array_clear(&a);
	vm_run(vm);
}

int __cdecl main(int argc, char** argv)
{
#ifdef TESTS
	run_tests();
#endif 
	if (argc > 1)	
	{
		VM vm;
		vm_init(&vm);
		load_and_run(&vm, argv[1]);
	}
}
